JavaScript 56.7%
TypeScript 42.6%
1import { NextRequest, NextResponse } from "next/server";2import { getIronSession } from "iron-session";3import { SessionData, sessionOptions } from "@/lib/session";4import { prisma } from "@/lib/prisma";5import { notifyFileComment } from "@/lib/notifications";67export async function GET(8 request: NextRequest,9 { params }: { params: { id: string } }10) {11 const response = NextResponse.next();12 const session = await getIronSession<SessionData>(request, response, sessionOptions);13 if (!session.isLoggedIn || !session.userId) {14 return NextResponse.json({ error: "Unauthorized" }, { status: 401 });15 }1617 const comments = await prisma.comment.findMany({18 where: { fileId: params.id },19 include: {20 user: {21 select: { id: true, name: true, username: true, avatarPath: true },22 },23 },24 orderBy: { createdAt: "asc" },25 });2627 return NextResponse.json(comments);28}2930export async function POST(31 request: NextRequest,32 { params }: { params: { id: string } }33) {34 const response = NextResponse.next();35 const session = await getIronSession<SessionData>(request, response, sessionOptions);36 if (!session.isLoggedIn || !session.userId) {37 return NextResponse.json({ error: "Unauthorized" }, { status: 401 });38 }3940 const { content } = await request.json();41 if (!content || !content.trim()) {42 return NextResponse.json({ error: "Le contenu est requis" }, { status: 400 });43 }4445 const file = await prisma.file.findUnique({46 where: { id: params.id },47 select: { id: true, name: true, userId: true },48 });49 if (!file) {50 return NextResponse.json({ error: "Fichier introuvable" }, { status: 404 });51 }5253 const comment = await prisma.comment.create({54 data: {55 fileId: params.id,56 userId: session.userId,57 content: content.trim(),58 },59 include: {60 user: {61 select: { id: true, name: true, username: true, avatarPath: true },62 },63 },64 });6566 // Notify other commenters + file owner67 const commenter = await prisma.user.findUnique({68 where: { id: session.userId },69 select: { name: true, username: true },70 });71 const commenterName = commenter?.name || commenter?.username || "Quelqu'un";7273 // Get all unique users who commented on this file (excluding the current user)74 const otherCommenters = await prisma.comment.findMany({75 where: { fileId: params.id, userId: { not: session.userId } },76 select: { userId: true },77 distinct: ["userId"],78 });7980 const usersToNotify = new Set(otherCommenters.map((c) => c.userId));8182 // Also notify file owner if they haven't commented83 if (file.userId && file.userId !== session.userId) {84 usersToNotify.add(file.userId);85 }8687 for (const userId of Array.from(usersToNotify)) {88 await notifyFileComment(userId, commenterName, file.name, file.id);89 }9091 return NextResponse.json(comment, { status: 201 });92}93